package com.dawnClass.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dawnClass.VO.NoteConsumerVO;
import com.dawnClass.mapper.ConsumerMapper;
import com.dawnClass.mapper.NoteMapper;
import com.dawnClass.mapper.VideoMapper;
import com.dawnClass.pojo.*;
import com.dawnClass.service.NoteService;
import com.dawnClass.service.VideoService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteMapper noteMapper;

    @Autowired
    private VideoMapper videoMapper;


    @Autowired
    private VideoService videoService;

    @Autowired
    private ConsumerMapper consumerMapper;


    /**查询章节小计
     *
     * @param chapterId
     * @param pageNum
     * @return
     */
    @Override
    public ServiceResponse getVideoComment(Integer chapterId ,
                                           Integer pageNum) {
        PageHelper.startPage(pageNum,2);
        NoteExample noteExample = new NoteExample();
        noteExample.createCriteria().andChapterIdEqualTo(chapterId);
        noteExample.setOrderByClause("update_time desc");
        List<Note> notes = noteMapper.selectByExampleWithBLOBs(noteExample);
        PageInfo resultList = new PageInfo(notes);
        List<NoteConsumerVO> noteConsumerVOS = new ArrayList<>();
        for (Note note:notes){
            Consumer consumer = consumerMapper.selectByPrimaryKey(note.getConsumerId());
            noteConsumerVOS.add(new NoteConsumerVO(note,consumer));
        }
        resultList.setList(noteConsumerVOS);
        if (noteConsumerVOS.size()>0)
            return ServiceResponse.getSuccess(resultList);
        return ServiceResponse.getFail(null);
    }

    @Override
    public ServiceResponse saveVideoComment(Note note) {

        int row = noteMapper.insert(note);
        if (row >0)
            return ServiceResponse.saveSuccess(row);
        return ServiceResponse.getFail(null);
    }


}
